package com.szsfm.vehiclemanagement.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhoubigbo on 2017/11/21.
 */
public class VehicleInfoView extends ViewGroup {

    private List<View> mViews;
    private int mWidth,mHeight;
    private Context mContext;

    public VehicleInfoView(Context context) {
        super(context);
        init(context);
    }

    public VehicleInfoView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        init(context);
    }

    private void init(Context context){
        this.mContext = context;
        mViews = new ArrayList<>();
        TextView textView = textViewStyle();
        textView.setText("车辆信息:");
        addView(textView);
        invalidate();
    }

    @Override
    protected void onLayout(boolean bool, int left, int top, int right, int bottom) {
        int childCount=getChildCount();
        int t = 0;
        mWidth = getWidth();
        mHeight = getHeight();
        for (int i=0;i<childCount;i++){
            View view=getChildAt(i);
            int h = 120;
            view.layout(5,t+=5,mWidth-10,t+h);
            t += h;
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        mWidth = getWidth();
//        int childCount = getChildCount();
//        for (int i=0;i<childCount;i++){
//            View view =getChildAt(i);
//            view.measure(mWidth-10,60);
//        }
    }

    public void addText(String txt){
        TextView textView = textViewStyle();
        textView.setText(""+txt);
        textView.setTextSize(24);
        addView(textView);
        invalidate();
    }

    private TextView textViewStyle(){
        TextView textView = new TextView(this.mContext);
        textView.setTextColor(Color.BLACK);
        textView.setGravity(Gravity.CENTER_VERTICAL);
        textView.setPadding(2,0,0,0);
        textView.setTextSize(18);
        textView.setLines(2);
        return textView;
    }


    public void clearText(){
        removeAllViews();
    }

    public void refresh(){
        invalidate();
    }

}
